package com.example.shop_2208.register

import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_2208.R
import com.example.shop_2208.WebViewActivity
import com.example.shop_2208.databinding.ActivityRegisterBinding
import com.example.shop_2208.login.LoginActivity
import com.example.shop_2208.mvvm.BaseActivity
import com.example.shop_2208.net.Const
@Route(path = "/app/register")
class RegisterActivity : BaseActivity<ActivityRegisterBinding,RegisterViewModel>() {
    override fun initData() {
        viewModel.registerSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==Const.CODE_SUCCESS){
                val intent = Intent(this,LoginActivity::class.java)
                intent.putExtra("username",it.data.username)
                intent.putExtra("password",it.data.password)
                setResult(102,intent)
                finish()
            }
        }
    }

    override fun initView() {
        binding.register.isEnabled = false //默认注册按钮不可用
        //给复选框添加监听事件
        binding.cb.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.register.isEnabled = binding.cb.isChecked
        }


        binding.register.setOnClickListener {
            viewModel.register(
                binding.etUsername.text.toString(),
                binding.etPassword.text.toString(),
                binding.etRepassword.text.toString()
            )
        }
        val span = SpannableStringBuilder(binding.cb.text.toString())
        span.setSpan(ForegroundColorSpan(Color.RED),6,12,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        val clickableSpan = object:ClickableSpan(){
            override fun onClick(widget: View) {
                /*ToastUtils.showLong("点击了复选框")*/
                //跳转到详情界面
                val intent = Intent(this@RegisterActivity,WebViewActivity::class.java)
                intent.putExtra("url","https://m.qfang.com/shenzhen/user/user-protocol.html")
                startActivity(intent)
            }
        }
        span.setSpan(clickableSpan,13,19,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.cb.movementMethod = LinkMovementMethod.getInstance()//移除本身的点击事件，能够响应富文本的点击事件
        binding.cb.text = span//给复选框重新复制

    }

    override fun getViewModel(): Class<RegisterViewModel> = RegisterViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_register

}